package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3TaskShortAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<String> mList;

    private Context mContext;

    public X3TaskShortAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
    }

    public void changeShort(List<String> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new TaskShortAddHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_task_short_add_item,parent,false));
        }
        return new TaskShortHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_task_short_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == 0) {
            TaskShortAddHolder addHolder = (TaskShortAddHolder) holder;
            addHolder.tv_add.setOnClickListener(v -> {
                if (iOnClearStr != null) {
                    iOnClearStr.iOnAddShortStr();
                }
            });

        }else {

            TaskShortHolder shortHolder = (TaskShortHolder) holder;
            shortHolder.tv_content.setText(mList.get(position));
            shortHolder.iv_clear.setOnClickListener(v -> {
                if (iOnClearStr != null) {
                    iOnClearStr.iOnClearShortStr(mList.get(position));
                }
            });
            shortHolder.tv_content.setOnClickListener(v -> {
                if (iOnClearStr != null) {
                    iOnClearStr.iOnSelectShortStr(mList.get(position));
                }
            });
        }


    }

    @Override
    public int getItemViewType(int position) {
        if (X3StringUtils.isListEmpty(mList)) {
            return 0;
        }
        return 1;
    }

    IOnClearStr iOnClearStr;

    public void setiOnClearStr(IOnClearStr iOnClearStr) {
        this.iOnClearStr = iOnClearStr;
    }

    public interface IOnClearStr {
        void iOnClearShortStr(String content);

        void iOnSelectShortStr(String content);

        void iOnAddShortStr();
    }

    @Override
    public int getItemCount() {
        return X3StringUtils.isListEmpty(mList) ? 1 : mList.size();
    }

    class TaskShortHolder extends RecyclerView.ViewHolder {
        TextView tv_content;
        ImageView iv_clear;
        public TaskShortHolder(@NonNull View itemView) {
            super(itemView);
            tv_content = itemView.findViewById(R.id.tv_content);
            iv_clear = itemView.findViewById(R.id.iv_clear);
        }
    }

    class TaskShortAddHolder extends RecyclerView.ViewHolder {
        TextView tv_add;
        public TaskShortAddHolder(@NonNull View itemView) {
            super(itemView);
            tv_add = itemView.findViewById(R.id.tv_add);
        }
    }
}
